<template>
  <div class="q-pa-sm q-gutter-y-sm">
    <!--        Loading-->
    <q-linear-progress stripe indeterminate style="height: 1px" v-show="loading"/>
    <!--        toolbar-->
    <q-toolbar class="text-primary">
      <q-btn flat round dense icon="home" @click="onClickLeft"/>
      <q-toolbar-title>
        登陆
      </q-toolbar-title>
    </q-toolbar>
        <div>
          <q-input label="用户名" placeholder="请输入用户名" v-model="intUser.userName"/>
          <q-input label="密码" placeholder="密码" v-model="intUser.password" @keyup.13="regLogin" />
          <q-btn color="primary" style="margin: 10px;"  :label="regStr[regType]" @click="regLogin"/>
<!--          <q-btn  :label="regChangeStr[regType]"  style="margin: 10px;float: right;"  @click="changeRegType"/>-->
        </div>
    </div>
</template>
<script>
import axios from 'axios'
import { nlocalstorage } from '../util/localstorage.js'
export default {
  name: 'login',
  data () {
    return {
      intUser: {},
      loading: false,
      regStr: {
        1: '登陆/注册',
        0: '登陆/注册'
      },
      regType: 0,
      regChangeStr: {
        1: '注册',
        0: '已有账号？去登陆'
      },
      regUrl: {
        1: 'intUser/login',
        0: 'intUser/login'
      }
    }
  },
  mounted () {
  },
  methods: {
    regLogin () {
      const _this = this
      const url = this.regUrl[this.regType]
      axios({
        url: url,
        params: _this.intUser,
        validateStatus: function (status) {
          return status >= 200 && status <= 303
        }
      }).then(res => { // 登陆/注册成功
        if (res.data.state === 'success') {
          _this.$q.notify('成功')
          _this.$simpleStore.setUserInfo(res.data.obj)
          nlocalstorage.set('intUserToken', res.data.obj.intUserToken)
          _this.onClickLeft()
        } else {
          _this.$q.notify(res.data.message)
        }
      }).catch(e => {
        // eslint-disable-next-line no-console
        console.log(e)
        _this.$q.notify('服务器异常')
      })
      _this.intUser = {}
    },
    onClickLeft () {
      this.$router.replace('/').catch(() => {})
    },
    changeRegType () {
      this.regType = this.regType ^ 1
    }
  }
}
</script>

<style scoped>

</style>
